<template>
  <div class="update-birthday">
    <van-datetime-picker
      :max-date="maxDate"
      :min-date="minDate"
      @cancel="$emit('close')"
      @confirm="onConfirm"
      title="日期"
      type="date"
      v-model="currentDate"
    />
  </div>
</template>
<script>
import dayjs from 'dayjs'
import { updateUserProfile } from '@/api/user'
export default {
  name: 'UpdateBirthday',
  props: {
    value: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      minDate: new Date(1970, 0, 1),
      maxDate: new Date(),
      currentDate: new Date(this.value),
    }
  },
  methods: {
    async onConfirm () {
      this.$toast.loading({
        message: '保存中',
        forbidClick: true,
        duration: 0
      })
      try {
        const currentDate = dayjs(this.currentDate).format('YYYY-MM-DD')
        const { data } = await updateUserProfile({
          birthday: currentDate
        })
        console.log(data);
        this.$emit('input', currentDate)
        this.$emit('close')

        this.$toast.success('更新成功')
      } catch (err) {
        this.$toast.fail('更新失败')
      }
    }
  }
}
</script>

<style lang="less" scoped>
</style>